package EGS.impl;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.ExecutorService;

import org.dom4j.Document;
import org.dom4j.Element;
import TOOLS.SQLtool;
import TOOLS.errCodeManagement;
import WORKPLACE.stationClient;

public class EGS_execute_service552Impl{
	public boolean isUnknowCase = false;
	ExecutorService iExecutorService;
	Document MSG;
	boolean isPanel = false;
	stationClient iClient=null;
	SQLtool sql = null;
	/**
	 * 552 多条码复用550
	 */
	public EGS_execute_service552Impl(stationClient nClient, Document MSG_in, ExecutorService executorService, 
			SQLtool goldenSql) throws Exception {
		super();
		sql=goldenSql;
		if(nClient!=null) {
			iClient=nClient;
		}else {
			iClient=null;
		}
		MSG = MSG_in;
		iExecutorService = executorService;
	}

	public Document runCal() {
		String msgClass = "";
		try {
			if (MSG == null) {
				return null;
			}
			Element Mroot = MSG.getRootElement();
			msgClass = Mroot.element("header").attribute("messageClass").getValue();
			String transactionID = Mroot.element("header").attribute("transactionID").getValue();
			String iStation, iSN, iLayer,iState;

			iStation = Mroot.element("header").element("location").attribute("equipmentName").getValue();

			if (msgClass.equals("552")) {//
				Element eleCheckList = ((Element) Mroot.clone()).element("body");
				Mroot.remove(Mroot.element("body"));
				Mroot.addElement("body");
				Vector<Thread> uplTs=new Vector<Thread>();
				List<?> bodySons = eleCheckList.elements();//panel字段
				if (bodySons.size() > 0) {
					int listLen = bodySons.size();
					for (int listI = 0; listI < listLen; listI++) {
						Element eleR = Mroot.element("body").addElement("result");
						String iRepair = "0";
						iSN=((Element) (bodySons.get(listI))).attribute("pcbID").getValue();
						iLayer=((Element) (bodySons.get(listI))).attribute("productSide").getValue();
						iState=((Element) (bodySons.get(listI))).attribute("state").getValue();
						try {
							iRepair = ((Element) (bodySons.get(listI))).attribute("repairType").getValue();
						} catch (Exception e) {
							iRepair = "0";
						}
						Thread uplT=uploadPCBs(iStation,iSN,iLayer,iState,iRepair,transactionID,eleR);
						uplTs.add(uplT);
					}
					for (int listI = 0; listI < listLen; listI++) {
						uplTs.get(listI).join();
					}
					AKSLogs(5521, "" + transactionID + "_" + iStation );
				}
				
			} else {
				isUnknowCase = true;
				return null;
			}
			return MSG;
		} catch (Exception e) {
			if(iClient==null) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", "工位信息获取失败");
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			String str = errCodeManagement.getErrText(e.getMessage());
			System.out.println(str);
			return null;
		}

	}

	public void AKSLogs(int iType, String ilogClientNews) {
		SQLtool.AKSLogs(iType, ilogClientNews);
	}
	
	private Thread uploadPCBs(String iStation,String iSN, String iLayer, String iState, 
			String iRepair,String transactionID, Element eleR) throws Exception {
		String iiSN=iSN;
		Thread iTH1 = new Thread(new Runnable() {
			String oSN=iiSN;
			@Override
			public void run() {
				eleR.addAttribute("pcbID", oSN);
				try {
					int iiState=0;
					try {
						iiState = Integer.parseInt(iState);
					}catch(Exception e) {
						String str = errCodeManagement.getErrText("过站电文异常body->panel->state_"+e.getMessage());
						throw new Exception(str);
					}
					String oLayer=iLayer.replaceAll(" ", "");
					int iiLayer = Integer.parseInt(oLayer);
					if(iiLayer==-1) {
						iiLayer=iClient.myLayer;
					}
					if (iiLayer != iClient.myLayer) {
						String str = "工单激活面别(" + iClient.myLayer + ")与产品上传面别不一致！";
						throw new Exception(str);
					} else {
						boolean bState = false;
						if (iiState == 0) {
							bState = true;
						}
						boolean bRepair = false;
						if (iRepair.equals("1")) {
							bRepair = true;
						}
						boolean icallXRay = false;
						try {
							iClient.uploadState(oSN, bState, bRepair, icallXRay,"-1");// 如果工序是A0200强制重回FCT，除非没有FCT工序
						} catch (Exception ex) {
							if (ex.getMessage().equals("-152")) {
								String realSN = iClient.getSN4Label(oSN);// 如果条码未识别，先查询看看是否为标签
								if (realSN != null) {
									oSN = realSN;
									iClient.uploadState(oSN, bState, bRepair, icallXRay,"-1");
								} else {
									throw ex;
								}
							} else {
								throw ex;
							}
						}
					}
					
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "");
					AKSLogs(5501, "" + oSN + "_" + iStation + "_" + iLayer);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					AKSLogs(5500, "" + oSN + "_" + iStation + "_" + iLayer + "_" + str);
				}
				eleR.addAttribute("actionCode", transactionID);
			}
		});
		iTH1.start();
		
		return iTH1;
		
		
		
	}

}
